//
//  MMImagesCell.m
//  MemeMaker
//
//  Created by Phan Quang Ha on 7/23/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMImagesCell.h"
#import "MMFile.h"

#import "MMImageView.h"
#import <QuartzCore/QuartzCore.h>

@implementation MMImagesCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void)awakeFromNib
{
    [super awakeFromNib];
//    self.imgView1.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    self.imgView2.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    self.imgView3.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    self.imgView1.layer.borderWidth = 1;
//    self.imgView2.layer.borderWidth = 1;
//    self.imgView3.layer.borderWidth = 1;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (BOOL)setImages:(NSArray *)arrImages
{
    if (arrImages.count >= 1) {
        self.imgView1.image = [arrImages objectAtIndex:0];
        if (arrImages.count >= 2) {
            self.imgView2.image = [arrImages objectAtIndex:1];
            if (arrImages.count >= 3) {
                self.imgView3.image = [arrImages objectAtIndex:2];
            }else{
                self.imgView3.image = nil;
            }
        }else{
            self.imgView2.image = nil;
        }
        return YES;
    }else{
        self.imgView1.image = nil;
        return NO;
    }
}
- (BOOL)setImagesWithPaths:(NSArray *)paths
{
    if (paths.count >= 1) {
        self.imgView1.name = [[[paths objectAtIndex:0] lastPathComponent] stringByDeletingPathExtension];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            
            UIImage *image = [UIImage imageWithContentsOfFile:[paths objectAtIndex:0]];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.imgView1.image = image;
            });
            
        });
        
        if (paths.count >= 2) {
            self.imgView2.name = [[[paths objectAtIndex:1] lastPathComponent] stringByDeletingPathExtension];
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                
                UIImage *image = [UIImage imageWithContentsOfFile:[paths objectAtIndex:1]];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.imgView2.image = image;
                });
                
            });
            if (paths.count >= 3) {
                self.imgView3.name = [[[paths objectAtIndex:2] lastPathComponent] stringByDeletingPathExtension];
                
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                    
                    UIImage *image = [UIImage imageWithContentsOfFile:[paths objectAtIndex:2]];
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.imgView3.image = image;
                    });
                    
                });
            }else{
                self.imgView3.image = nil;
            }
        }else{
            self.imgView2.image = nil;
        }
        return YES;
    }else{
        self.imgView3.image =nil;
        return NO;
    }
}
- (BOOL)setImagesWithFiles:(NSArray *)files
{
    if (files.count >= 1) {
        self.imgView1.image = [UIImage imageWithContentsOfFile:[[files objectAtIndex:0] filePath]];
        self.imgView1.name = [[[[files objectAtIndex:0] filePath] lastPathComponent] stringByDeletingPathExtension];
        if (files.count >= 2) {
            self.imgView2.image = [UIImage imageWithContentsOfFile:[[files objectAtIndex:1] filePath]];
            self.imgView2.name = [[[[files objectAtIndex:1] filePath] lastPathComponent] stringByDeletingPathExtension];
            if (files.count >= 3) {
                self.imgView3.image = [UIImage imageWithContentsOfFile:[[files objectAtIndex:2] filePath]];
                self.imgView3.name = [[[[files objectAtIndex:2] filePath] lastPathComponent] stringByDeletingPathExtension];
            }else{
                self.imgView3.image = nil;
            }
        }else{
            self.imgView2.image = nil;
        }
        return YES;
    }else{
        self.imgView1.image = nil;
        return NO;
    }
}


#pragma mark - Touch Events
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    if (_delegate && [_delegate respondsToSelector:@selector(didTouchImage:withName:)]) {
        if (CGRectContainsPoint(self.imgView1.frame, touchPoint)) {
            [_delegate didTouchImage:self.imgView1.image withName:self.imgView1.name];
            return;
        }
        if (CGRectContainsPoint(self.imgView2.frame, touchPoint)) {
            [_delegate didTouchImage:self.imgView2.image withName:self.imgView2.name];
            return;
        }
        if (CGRectContainsPoint(self.imgView3.frame, touchPoint)) {
            [_delegate didTouchImage:self.imgView3.image withName:self.imgView3.name];
            return;
        }
    }
    
}

@end
